package com.rgbgame.launcher;

import processing.core.PApplet;
import processing.core.PFont;

import com.rgbgame.game.rgbGame;
import com.rgbgame.editor.rgbEditor;

/**
 * This is the PApplet launcher class, which launches the Processing
 * instance and passes it along to the other components.
 * Date: Mar 9, 2008
 */
public class Launcher extends PApplet {

    public static final int GAME = 1;
    public static final int EDITOR = 2;

    public static int current = EDITOR;

    private rgbGame game = null;
    private rgbEditor editor = null;

    public void launch(int type) {
        switch (type) {
            case GAME:
                current = GAME;
                if (game == null) game = new rgbGame();
                if (editor != null) game.setup(this, editor.print());
                else game.setup(this);
                return;
            case EDITOR:
                current = EDITOR;
                if (editor == null) editor = new rgbEditor();
                if(game != null) editor.setup(this, game.print());
                else editor.setup(this);
                
        }
    }

    public void switchApp() {
        switch (current) {
            case GAME:
                current = EDITOR;
                PFont font = loadFont("LucidaGrande-Bold-14.vlw");
                textFont(font, 14);
                return;
            case EDITOR:
                current = GAME;
        }
    }

    public void setup() {
        //size(1024, 768, PApplet.P3D);
    	size(800,600, PApplet.P3D);
        frameRate(60);
        background(0);
        launch(current);
    }

    public void draw() {
        switch (current) {
            case GAME:
                if (game != null) game.draw();
                return;
            case EDITOR:
                if (editor != null) editor.draw();
        }
    }

    public void keyPressed() {
        switch (current) {
            case GAME:
                if (game != null) game.keyPressed(keyCode, key);
                return;
            case EDITOR:
                if (editor != null) editor.keyPressed(keyCode, key);
        }
    }

    public void keyReleased() {
        switch (current) {
            case GAME:
                if (game != null) game.keyReleased(keyCode, key);
                return;
            case EDITOR:
                if (editor != null) editor.keyReleased(keyCode, key);
        }
    }

    public void mousePressed() {
        if (current == GAME && game != null) game.mousePressed();
    }

    public void mouseReleased() {
        if (current == GAME && game != null) game.mouseReleased();
    }


    static public void main(String args[]) {
        PApplet.main(new String[]{"com.rgbgame.launcher.Launcher"});
    }
}
